<?php

namespace App\Services\TransferBrokerageToBalance;

use App\Models\User;
use Illuminate\Support\Facades\DB;

class TransferBrokerageToBalance
{
    protected User $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    protected function transfer()
    {
        DB::transaction(function () {
            $brokerage = $this->user->dividend_brokerage;
            account_brokerages($this->user)->minusTransferedOutBrokerage($brokerage);
            account_balances($this->user)->plusTransferedBalance($brokerage);
        });
    }

    public function apply()
    {
        $this->transfer();
    }
}
